package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.User;
import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.entity.News;
import cn.jdy.platform.service.ConfService;
import cn.jdy.platform.service.NewsService;
import cn.jdy.plugins.entity.PptImg;
import cn.jdy.plugins.service.LiveService;
import cn.jdy.plugins.service.PptImgService;

/**
 * @title 新闻信息控制类
 * @date 2014-03-12
 */
@Controller
@RequestMapping("/pptImg")
public class PptImgCtrl extends BaseCtrl {

	@Autowired
	private PptImgService pptImgService;
	@Autowired
	private LiveService LiveService;
	//默认列表路径
	private static String viewName = "redirect:/ppt/list?s=";

	/**
	 * 初始化
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(News entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/operate/newsList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
		mv.addObject("entity", entity);
		mv.addObject("fromDate", fromDate);
		mv.addObject("toDate", toDate);
		mv.addObject("classList", confService.queryByType(GlobalConstant.TYPE_NEWS, 0L));
		return mv;
	}
	
	
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}
	
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/ppt/pptDetail.jsp");

		  DetachedCriteria dc = DetachedCriteria.forClass(PptImg.class);
		  if(!StringUtil.isNullOrEmpty(id)){
			  dc.add(Restrictions.eq("pptListid", id));
		  }
			dc.addOrder(Order.asc("sortTop"));
			dc.addOrder(Order.asc("sortLeft"));
		mv.addObject("listDate",  pptImgService.find(dc));
	//	mv.addObject("liveid", id);
		mv.addObject("pptListid", id);
		return mv;
	}
	

	/**
	 * 保存（更新）
	 * @param news
	 * @return
	 */
	@RequestMapping("/save")
	 @ResponseBody
	public Map<String, Object> save(PptImg obj,String sortTops,String sortLefts) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			  if(obj!=null){
				  if(obj.getPptimg()!=null){
					//  pptImgService.delete("liveid", obj.getLiveid());
					  pptImgService.delete("pptListid", obj.getPptListid());
					 String img[]= obj.getPptimg().split(",");
					   for (int i = 0; i < img.length; i++) {
						   PptImg pt=new PptImg();
						   pt.setPptListid(obj.getPptListid());
						   pt.setPptimg(img[i]);
						   pt.setSortTop(Integer.valueOf(sortTops.split(",")[i]));
						   pt.setSortLeft(Integer.valueOf(sortLefts.split(",")[i]));
						   //pt.setIssort(i+1);
						   pt.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
						   pt.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
						   
						   pptImgService.save(pt);
					}
				  }
			  }
			    jsonMap.put("statusCode", 200);
	} catch(Exception e) {
	    jsonMap.put("statusCode",400);
		BaseLog.e(getClass(), "保存异常！", e);
	}
		
		return jsonMap;
	}
	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView();
		try {
			this.pptImgService.delete(id);
			mv.setViewName(this.viewName.concat("3"));
			// 清理文章缓存
      NginxPurgeCacheUtil.purge(2, id);
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "删除新闻异常！", e);
		}
		return mv;
	}
	

}
